# -*- coding: utf-8 -*-
from urllib.error import HTTPError, URLError
from http.client import IncompleteRead, RemoteDisconnected
import timeit, time
import urllib.request
import socket

timeout = 5
socket.setdefaulttimeout(timeout) 


def getUrls(path):
    urls = []
    with open(path, 'r') as f:
        for line in f:
            urls.append(line.strip('\n'))
    return (urls)


def clock(func):  # 修饰器函数，对函数计时
    def clocked(*args):
        t0 = timeit.default_timer()
        result = func(*args)
        elapsed = timeit.default_timer() - t0
        name = func.__name__
        arg_str = ', '.join(repr(arg) for arg in args)
        # print('[%0.8fs] %s(%s)' % (elapsed, name, arg_str))
        # print('%s(%s) -> %r [%0.8fs]' % (name, arg_str, result, elapsed))
        return result

    return clocked


@clock
def requestImg(url, name, num_retries=3):
    img_src = url
    # print("requestImg: " + img_src)
    header = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) \
        AppleWebKit/537.36 (KHTML, like Gecko) \
            Chrome/35.0.1916.114 Safari/537.36',
        'Cookie': 'AspxAutoDetectCookieSupport=1'
    }
	
    req = urllib.request.Request(url=img_src, headers=header)
    # print("requestImg: hahah ")
    try:
        response = urllib.request.urlopen(req)
        filename = name #+ '.jpg'
        with open(filename, "wb") as f:
            content = response.read()
            f.write(content)
            response.close()
    except HTTPError as e:
        print("HTTPError: " + e.reason)
    except URLError as e:
        print("URLError: " + e.reason)
    except IncompleteRead or RemoteDisconnected as e:
        if num_retries == 0:
            print("num_retries == 0")
            return
        else:
            print("requestImg(url, name, num_retries - 1)...")
            requestImg(url, name, num_retries - 1)


@clock
def requestTxt(url, name, num_retries=3):
    img_src = url
    # print(img_src)
    header = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) \
        AppleWebKit/537.36 (KHTML, like Gecko) \
            Chrome/35.0.1916.114 Safari/537.36',
        'Cookie': 'AspxAutoDetectCookieSupport=1'
    }
    req = urllib.request.Request(url=img_src, headers=header)
    try:
        response = urllib.request.urlopen(req)
        filename = name + '.txt'
        with open(filename, "wb") as f:
            content = response.read()
            f.write(content)
            response.close()
    except HTTPError as e:
        print(e.reason)
    except URLError as e:
        print(e.reason)
    except IncompleteRead or RemoteDisconnected as e:
        if num_retries == 0:
            return
        else:
            requestTxt(url, name, num_retries - 1)


if __name__ == '__main__':
    #urls = getUrls('https://offlintab.firefoxchina.cn/static/img/search/baidu_web.png') # 换成你的URL文件路径
    urls = []
    urls.append('http://0.0.0.0:8000/101_郭良磊_0_101_0.jpg')
    nLines = len(urls)
    print(nLines)
    for index, value in enumerate(urls):
        #requestImg(value, './' + str(index).zfill(6))  # zfill用来格式化数字：000001012
        requestImg(value, './' + "tmp")  # zfill用来格式化数字：000001012
